/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.user;

import com.ivanbiz.account.AccountService;
import com.ivanbiz.departement.DepartementService;
import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.kelas.KelasService;
import com.ivanbiz.properties.DepartementException;
import com.ivanbiz.properties.PasswordException;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.properties.UserNameException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class LoginAction extends org.apache.struts.action.Action {

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        ActionMessages actionMessages = new ActionMessages();
        HttpSession session = request.getSession(true);
        UserService userService = (UserService) CallCtx.getContext(request).getBean(SISBeanConstants.USER_SERVICE);
        DepartementService departementService = (DepartementService) CallCtx.getContext(request).getBean(SISBeanConstants.DEPARTEMENT_SERVICE);

        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        UserForm userForm = (UserForm) form;
        User user;
        try {
            user = userService.login(userForm.getUserName(), userForm.getPassword(), userForm.getDepartement());
            session.setAttribute(SISConstants.KELAS, kelasService.loadByUserId(user.getId()));
            session.setAttribute(SISConstants.USER, user);
            session.setAttribute(SISConstants.DEPARTEMENT_ID, userForm.getDepartement());
        } catch (UserNameException userNameException) {
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.USERNAME_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        } catch (PasswordException passwordException) {
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.PASSWORD_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        } catch (DepartementException departementException) {
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.DEPARTEMENT_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        } catch (Exception ex) {
            Logger.getLogger(LoginAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.SUCCESS);
    }
}
